package utils

import java.net.URI
import java.nio.charset.Charset

import scala.util.Try

object EncoderEx {

  def encodeURI(uriStr: String, charset: Charset): Try[String] = {
    Try {
      val uri = URI.create(uriStr)
      val classType = uri.getClass

      // 获取 Method 对象
      val method = classType.getDeclaredMethod("defineString")
      // 获取 Field 对象
      val field = classType.getDeclaredField("string")

      method.setAccessible(true)
      field.setAccessible(true)

      method.invoke(uri)
      val str = field.get(uri).asInstanceOf[String]
      Enc.encode(str, charset)
    }
  }

}
